Meteor.startup(function () {
	
});

Accounts.onCreateUser(function(options, user) {
	user.profile = options.profile || {};
	user.created = new Date();
	if ( !Meteor.users.find().count() )
		user.isAdmin = true;
	return user;
});
Meteor.publish("topics", function (page, category) {
	// ** todo: 需要增加对非整数负数判断
	// console.log(category === null);
	if (page === null || page === 'undefined')
		page = 1;
	var ITEM = 3;
	var start = (page-1)*ITEM;
	// console.log("page = " + page + "item = " + ITEM +"START = " + start);
	if (category !== null && category !== undefined) {
		return Topics.find({categories: category},{sort: {updated: -1}, skip:start, limit: ITEM});
	} else {
		return Topics.find({},{sort: {updated: -1}, skip:start, limit: ITEM});
	}
	
});
Meteor.publish("replys", function (tid) {
	
	return Replys.find();
});
Meteor.publish("users", function () {
	return Meteor.users.find({});
});
// Meteor.publish("memberTopics", function (memberId) {
// 	Topics.find({ userId: memberId}, {sort: {updated: -1}, limit: 20});
// });
// Meteor.publish("memberReplys", function (memberId) {
// 	Replys.find({userId: memberId}, {sort: {updated: -1}, limit:20});
// })
Meteor.publish("all_category", function () {
	return Category.find();
})
Meteor.methods({
	post: function (post) {
		var body;
			user = Meteor.user(),
			userId = post.userId || user._id;
	}
});
Meteor.startup(function () {
	if (Category.find().count() === 0 ){
		category = ['闲聊', '清水', 'acg'];
		for (var i = 0, l = category.length; i < l; i++) {
			Category.insert({name: category[i], created: new Date()});
		}
	}
});